package com.bj58.data.emailreports.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.bj58.data.emailreports.dao.CommonDao;
import com.bj58.data.emailreports.model.DataSourceCheck;
import com.bj58.data.emailreports.model.EmailReportUser;
import com.bj58.data.emailreports.service.EmailReportUserService;
import com.bj58.groupbuy.service.impl.BaseService;

public class EmailReportUserServiceImpl extends BaseService<EmailReportUser> implements EmailReportUserService {

	private CommonDao comDao = new CommonDao();
	
	public List<EmailReportUser> getListByCondition(String condition)
			throws Exception {
		List<EmailReportUser>  rsList = getGenericDao().find(EmailReportUser.class, "*", condition, "");
		if(null != rsList  && rsList.size()> 0){
			return  rsList;
		}
		return null;
	}

	@Override
	public Map<String, Integer> checkDataSource(DataSourceCheck checkVo) throws Exception{
		
		
		StringBuffer sql = new StringBuffer();
		sql.append(" select "+checkVo.getDataColName()+",count(1) ");
		sql.append(" from "+checkVo.getTableName().replace("*", "")+" ");
		sql.append(" where "+checkVo.getDataColName()+" >='"+checkVo.getCheckStartDate()+"'  ");
		sql.append(" and "+checkVo.getDataColName()+" <='"+checkVo.getCheckEndDate()+"'  ");
		sql.append(" group by  "+checkVo.getDataColName()+" ");
		
		
		Map<String,String> tempMap = comDao.selectValueAsMap(sql.toString(), checkVo.getDbid());
		
		Map<String, Integer> rsMap = new HashMap<String, Integer>();
		
		for(Iterator<String> it = tempMap.keySet().iterator(); it.hasNext();){
			String key = it.next();
			String v = tempMap.get(key);
			rsMap.put(key, Integer.parseInt(v));
		}
		
		return rsMap;
	}


}
